package com.cays.myresearch.action;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.Toast;

import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.Person;
import com.cays.myresearch.entity.Student;
import com.cays.myresearch.entity.UserInfo;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.Response;

public class LoginAction {
    private static LoginAction loginAction;
    public LoginAction(){}
    public static LoginAction getInstance(){
        if (loginAction==null){
            loginAction=new LoginAction();
        }
        return loginAction;
    }
    /**
     * 保存自动登录的用户信息
     */
    public void saveUserInfo(Context context,String name,String password,int type,boolean autoLogin,String user){
        SharedPreferences sp=context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name",name);
        editor.putString("password",password);
        editor.putInt("type",type);
        editor.putBoolean("autoLogin",autoLogin);
        editor.putString("user",user);
        editor.apply();

    }
    /**
     * 保存自动登录的用户信息
     */
    public void saveUserInfo(Context context,UserInfo userInfo){
        SharedPreferences sp=context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name",userInfo.getName());
        editor.putString("password",userInfo.getPassword());
        editor.putInt("type",userInfo.getType());
        editor.putBoolean("autoLogin",userInfo.getAutoLogin());
        editor.putString("user",userInfo.getUser());
        editor.apply();

    }
    /**
     *  获取用户信息model
     */
    public UserInfo getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        UserInfo userInfo=new UserInfo();
        userInfo.setName(sp.getString("name",""));
        userInfo.setPassword(sp.getString("password",""));
        userInfo.setType(sp.getInt("type",Constant.user_none));
        userInfo.setAutoLogin(sp.getBoolean("autoLogin",false));
        userInfo.setUser(sp.getString("user",""));
        return userInfo;
    }/**
     * userInfo中是否有数据
     */
    public boolean hasUserInfo(Context context) {
        UserInfo userInfo = getUserInfo(context);
        if (userInfo != null) {
            if ((!TextUtils.isEmpty(userInfo.getName())) &&
                    (!TextUtils.isEmpty(userInfo.getPassword()))&&
                    (!TextUtils.isEmpty(userInfo.getUser()))) {//有数据
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    //删除用户信息
    public void deleteUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name","");
        editor.putString("password","");
        editor.putInt("type", Constant.user_none);
        editor.putBoolean("autoLogin",false);
        editor.putString("user","");
        editor.apply();
    }
    public String getName(Context context){
        return this.getUserInfo(context).getName();
    }
    public int getType(Context context){
        return this.getUserInfo(context).getType();
    }
}
